/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChateoFacadeGUI;

import ChateoController.ChatViewController;
import ChateoController.ChatMessage;
import ChateoController.*;
import ChateoGUI.*;
import java.io.File;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 *
 * @author adrian
 * @author olga
 */
public class FacadeGUI {

    private Controller controller;
    private static ChatViewController gui;

    public FacadeGUI() {
        controller = Controller.getCentralController();

    }

    public FacadeGUI(Controller father) {
        controller = father;
    }

    /**
     *
     * GUI --> Controller
     *
     */
    /**
     * This method is the connection interface between the GUI and the System
     *
     * @param login
     * @param port
     * @throws NullPointerException
     * @throws SocketException
     * @throws UnknownHostException
     */
    public void processConneccion(String login, int port) throws NullPointerException, SocketException, UnknownHostException {
        gui = Controller.getcvController();
        Controller.getCentralController().connectFromGUI(port, login);
    }

    /**
     * This method is the disconnection interface between the GUI and the System
     *
     * @throws UnknownHostException
     * @throws SocketException
     */
    public void processDisconneccion() throws UnknownHostException, SocketException {
        controller.disconnect();
    }

    /**
     *
     * @param message is the public message to send
     * @throws SocketException
     * @throws UnknownHostException
     */
    public void sendPublicMessage(String message) throws SocketException, UnknownHostException {
        boolean privato = false;
        controller.sendMessage(null, message, privato);

    }

    /**
     *
     * @param user the destination user
     * @param message the private message to send
     * @throws SocketException
     * @throws UnknownHostException
     */
    public void sendPrivateMessage(User user, String message) throws SocketException, UnknownHostException {
        boolean privato = true;
        controller.sendMessage(user, message, privato);
    }

    public void sendFile(File selectedFile, User user) {

    }

    public void acceptFile() {

    }

    public void refuseFile() {

    }

    public void cancelFile() {

    }

    public ArrayList<ChatMessage> getMessages(User user) {

        return controller.getMessagesByUser(user);

    }

    /**
     *
     * Controller To GUI
     *
     * @param userList the list of users
     */
    public void updateUsers(UsersList userList) {

        gui.updateUserList(userList);
    }

    public void updateMessages(ArrayList<ChatMessage> messageList) {

        gui.updateMessagesOnSend(messageList);
    }

    public void refreshMessages(ArrayList<ChatMessage> messageList, User user) {
        gui.updateMessagesOnReceive(messageList, user);
    }

    public void updateTransfer() {

    }

    public void updateGUIafterConnection() {

    }

    public void updateGUIafterDisconnection() {

    }

    /**
     * LAUNCHING GUI
     *
     *
     * public void start(Stage stage) {
     *
     * try { FXMLLoader loader = new
     * FXMLLoader(getClass().getResource("/ChateoGUI/ChatRoom.fxml")); Parent
     * root = loader.load(); gui = loader.getController(); Scene scene = new
     * Scene(root);
     *
     * stage.setScene(scene); stage.show(); } catch (IOException ex) {
     * Logger.getLogger(FacadeGUI.class.getName()).log(Level.SEVERE, null, ex);
     * }
     *
     * }
     *
     *
     *
     * public void stop() throws Exception { gui.onExit();
     * super.stop(); }
     */
    public void guilauncher() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /**
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ChatGUI(Controller.getFacadegui()).setVisible(true);
            }
        });
    }

}
